package train.DAY625;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;

public class Client2 {
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket("127.0.0.1",9090);
        InputStream inputStream = socket.getInputStream();

        new Thread() {
            @Override
            public void run() {
                OutputStream os = null;


                while (true) {
                    //发信息
                    Scanner scanner = new Scanner(System.in);
                    String input = scanner.nextLine();//block

                    try {

                        os = socket.getOutputStream();
                        os.write(input.getBytes());
                        os.flush();

                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                    if (input.equals("quit")) {
                        break;
                    }
                }
            }
        }.start();


        //主线程收
        byte[] data = new byte[100];
        while (true) {
            try {
                inputStream.read(data);//block
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println("new String(data)  = " + new String(data));
        }

    }
}
